Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to respond to a message reaction being added or remove #520

Merged
merged 8 commits into from
Oct 28, 2024

Conversation

ArcticSnowman
Copy link
Contributor

@ArcticSnowman ArcticSnowman commented Oct 23, 2024

Proposed change

I worked on a slack chat bot that was based on https://github.com/ArcticSnowman/go-slackbot which I added the ability to respond to emojis (reaction).

So this is my first attempt to add reaction support to this bot framework. Hope this helps with #45

Types of changes

What types of changes is this pull request introducing to flottbot? Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

You can fill this out after creating your PR. Put an x in the boxes that apply

  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@coveralls
Copy link

coveralls commented Oct 24, 2024

Pull Request Test Coverage Report for Build 11555364501

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 0.0%

Totals Coverage Status
Change from base Build 10426782526: 0.0%
Covered Lines: 0
Relevant Lines: 0

💛 - Coveralls

Copy link
Collaborator

@wass3r wass3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the contribution! with a couple of caveats it works as advertised (see comments).

some further tweaks that would be interesting to explore are (and this definitely doesn't have to be in this PR):

  1. separate the action (added/removed) so you can target either
  2. check the target thread to see whether we already replied and, at least by default, don't post again

@@ -0,0 +1,11 @@
name: plus-one-reaction
active: true
reaction_match: ":+1:"
Copy link
Collaborator

@wass3r wass3r Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did this exact rule work for you?

asking because, 1) i can only make it work without the surrounding : and 2) only if the value doesn't contain +, like heavy_plus_sign works without problem.

the issue is that we send the the string to regex in the matcher where + has special meaning, and the :s aren't part of the reaction value returned by the Slack API - at least in my test workspace.

if you throw :+1: as instead of :hello: in the test you added you will notice that it fails or maybe you saw that too.

Suggested change
reaction_match: ":+1:"
reaction_match: "heavy_plus_sign"

i think to make +1 work we will need to make a few more tweaks and maybe bypass regex to do an exact match. we can do it in a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the example and fixed some issue I found. I decided that we need to treat addition and removals of reactions are separate in Rule and Message model so action can be triggered for one or other or both.

remote/slack/helper.go Outdated Show resolved Hide resolved
Update example
Some refactoring in matcher.go
Copy link
Collaborator

@wass3r wass3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works as advertised. thanks for the tweaks. couple of small things and we're good i think.

i noticed you used plural reactions_added vs reaction_added. was the thought to be able to support matching multiple reactions in one rule at some point? if so, i think i like that. some workspaces have multiple emojis that are similar, eg. :question: and :grey_question:. nothing that has to be fleshed out in this PR though. just curious.

thank you!

Makefile Outdated Show resolved Hide resolved
core/matcher.go Outdated Show resolved Hide resolved
core/matcher.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ArcticSnowman ArcticSnowman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should

	if rule.Hear == "" && rule.ReactionsAdded == "" && rule.ReactionsRemoved == "" {

not be better as

	if rule.Respond != "" {

@wass3r
Copy link
Collaborator

wass3r commented Oct 25, 2024

Should

	if rule.Hear == "" && rule.ReactionsAdded == "" && rule.ReactionsRemoved == "" {

not be better as

	if rule.Respond != "" {

makes a lot of sense, i like it! let's do that. if you don't mind changing the comment above it as well 🥺 thank you!

@ArcticSnowman
Copy link
Contributor Author

@wass3r - I went with plurals as the use case in the other bot library I use, does take multiple emoji triggers.

For this framework, the user would need to craft a regexp string to match multiple.

For example:

(plus_one|plusone|plus1)

@ArcticSnowman
Copy link
Contributor Author

PR for docs target/flottbot-docs#60

Comment on lines +241 to +242
//
//nolint:gocyclo // refactor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this addition is making the build and validate steps fail

@wass3r
Copy link
Collaborator

wass3r commented Oct 25, 2024

for the failing test (https://github.com/target/flottbot/actions/runs/11521113211/job/32081108474?pr=520#step:5:252), it looks like the improvement you made exposed a flaw in a testcase. in this block

testRuleNeedArg := models.Rule{}
testRuleNeedArg.AllowUsers = []string{"fooUser"}
testRuleNeedArg.Args = []string{"arg1", "arg2"}
testMessageNeedArg := new(models.Message)
needArgVars := make(map[string]string)
needArgVars["_user.name"] = "fooUser"
testMessageNeedArg.Vars = needArgVars
we will need to add testRuleNeedArg.Respond = "foo" (for example)

of Hear, Respond, ReactionsAdded, or ReactionsRremoved
Update matcher_tests
@ArcticSnowman
Copy link
Contributor Author

@wass3r - Yeah those unit tests need an explicit Hear/Respond added. I've clean up the test and added a check to the function that requires one of Hear, Respond, ReactionsAdded, or ReactionsRemoved. to have a value to be valid.

@wass3r
Copy link
Collaborator

wass3r commented Oct 25, 2024

i think we're one commit away.. linter is (harshly) complaining about this line

. happy to merge after that tiny edit. thanks for all the work!

@ArcticSnowman
Copy link
Contributor Author

Fixed to keep the linter happy...

On the topic of a rule checking if it's replied to the reaction already, the script/code handling the reaction would need to either

  • Be able to call the Slack (or other service) API to interrogate if the bot had responded
  • Keep some stateful data to log the TimeStamp of the message reacted to.

@wass3r
Copy link
Collaborator

wass3r commented Oct 28, 2024

yup, i was thinking about utilizing https://api.slack.com/methods/conversations.replies . trying to shy away from adding the complexity of state management for users/admins at this time :)

Copy link
Collaborator

@wass3r wass3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@wass3rw3rk wass3rw3rk merged commit 3bd3031 into target:main Oct 28, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants